Skip to content

[ValueTracking] Check both operands for being 0 and then the other for isKnownNonZero #147330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jul 7, 2025

We should check both operands, not just the first one.

@AZero13 AZero13 requested a review from nikic as a code owner July 7, 2025 15:45
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jul 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2025

@llvm/pr-subscribers-llvm-analysis

Author: AZero13 (AZero13)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/147330.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+6-5)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 09745ed6eac6a..a8f5728c940b1 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2880,11 +2880,6 @@ static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
   if (matchOpWithOpEqZero(X, Y))
     return true;
 
-  // TODO: Move this case into isKnownNonEqual().
-  if (auto *C = dyn_cast<Constant>(X))
-    if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
-      return true;
-
   return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
 }
 
@@ -3892,6 +3887,12 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2,
   if (Depth >= MaxAnalysisRecursionDepth)
     return false;
 
+  // 0 vs known-non-zero => definitely different
+  if (match(V1, m_Zero()) && isKnownNonZero(V2, DemandedElts, Q, Depth + 1))
+    return true;
+  if (match(V2, m_Zero()) && isKnownNonZero(V1, DemandedElts, Q, Depth + 1))
+    return true;
+
   // See if we can recurse through (exactly one of) our operands.  This
   // requires our operation be 1-to-1 and map every input value to exactly
   // one output value.  Such an operation is invertible.

@AZero13
Copy link
Contributor Author

AZero13 commented Jul 7, 2025

@dtcxzyw Can you please run the opt-benchmark so I can make tests

@AZero13 AZero13 changed the title Move 0 vs known-non-zero case to isKnownNonEqual [ValueTracking] Check both operands for being 0 and then the other for isKnownNonZero Jul 7, 2025
Copy link

github-actions bot commented Jul 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

if (match(V1, m_Zero()) && isKnownNonZero(V2, DemandedElts, Q, Depth + 1))
return true;
if (match(V2, m_Zero()) && isKnownNonZero(V1, DemandedElts, Q, Depth + 1))
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense. x - 0 is just x and should get folded away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants